package bg.tu.varna.pms.client.controller;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.SwingUtilities;

import org.omg.CORBA.portable.ApplicationException;

import bg.tu.varna.pms.client.ApplicationContext;
import bg.tu.varna.pms.client.gui.UserGroupPanel;
import bg.tu.varna.pms.client.listener.UserGroupListener;
import bg.tu.varna.pms.client.model.UserSearchListModel;
import bg.tu.varna.pms.model.communication.SearchMessage.Response;

public class UserGroupController extends MouseAdapter implements
		ActionListener, UserGroupListener {

	private final UserGroupPanel panel;

	public UserGroupController(UserGroupPanel panel) {
		this.panel = panel;
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		Component c = (Component) e.getSource();
		if (MypeActionCommand.SHOW_ADD_USER_PANEL.equals(c.getName())) {
			SwingUtilities.invokeLater(new Runnable() {
				@Override
				public void run() {
					panel.showAddUserDialog(UserGroupController.this);
				}
			});
		} else if (MypeActionCommand.SHOW_REMOVE_USER_PANEL.equals(c.getName())) {
			SwingUtilities.invokeLater(new Runnable() {
				@Override
				public void run() {
					panel.showRemoveUserDialog(UserGroupController.this);
				}
			});
		} else if (MypeActionCommand.SHOW_ADD_GROUP_PANEL.equals(c.getName())) {
			SwingUtilities.invokeLater(new Runnable() {
				@Override
				public void run() {
					panel.showAddGroupDialog(UserGroupController.this);
				}
			});
		} else if (MypeActionCommand.SHOW_REMOVE_GROUP_PANEL
				.equals(c.getName())) {
			SwingUtilities.invokeLater(new Runnable() {
				@Override
				public void run() {
					// TODO pass list model
					panel.showRemoveGroupDialog(UserGroupController.this);
				}
			});
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Component c = (Component) e.getSource();
		if (MypeActionCommand.ADD_GROUP.equals(e.getActionCommand())) {
			// TODO
		} else if (MypeActionCommand.ADD_USER.equals(e.getActionCommand())) {
			ApplicationContext.getApplicationContext().getUserContext()
					.addUser(panel.getUserNameToAdd(), null);
		} else if (MypeActionCommand.REMOVE_GROUP.equals(e.getActionCommand())) {
			// TODO
		} else if (MypeActionCommand.REMOVE_USER.equals(e.getActionCommand())) {
			ApplicationContext.getApplicationContext().getUserContext()
					.removeUser(panel.getUserNameToRemove());
		} else if (MypeActionCommand.FIND_USER.equals(e.getActionCommand())) {
			// TODO
			ApplicationContext
					.getApplicationContext()
					.getUserContext()
					.searchUsers(null, panel.getUserNameToFind(), null, null,
							null, null);
		}
	}

	@Override
	public void searchUsersComplete(Response result) {
		UserSearchListModel model = new UserSearchListModel(
				result.getContacts());
		panel.setUserSearchListModel(model);
	}

	@Override
	public void searchUsersError(ApplicationException e) {
		panel.setUsersError(e.getMessage());
	}
}
